package com.atguigu.boot.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.ResponseBody;
import sun.jvm.hotspot.HelloWorld;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * @author ：malichun
 * @date 2022/1/21 12:45 AM
 * @description：
 * @modified By：
 */
@Controller // 普通的控制器
public class RequestController {

    @GetMapping("/goto")
    public String goToPage(HttpServletRequest request){
        request.setAttribute("msg","成功了...");
        request.setAttribute("code","200");
        return "forward:/success"; // 转发到success的请求
    }



    @GetMapping("/params")
    public String testParam(Map<String,Object> map,
                            Model model,
                            HttpServletRequest request,
                            HttpServletResponse response){
        map.put("hello","world6666");
        model.addAttribute("world","hello6666");
        request.setAttribute("message", "HelloWorld");
        Cookie cookie = new Cookie("c1","v1");
        // cookie.setDomain("localhost");
        response.addCookie(cookie);

        return "forward:/success";
    }


    @ResponseBody
    @GetMapping("/success")
    public Map<String,Object> success(@RequestAttribute(value = "msg",required = false) String msg,
                                      @RequestAttribute(value ="code", required = false) Integer code,
                                      HttpServletRequest request
    ){
        Object msg1 = request.getAttribute("msg");
        Integer code1 = (Integer)request.getAttribute("code");
        Map<String,Object> map = new HashMap<>();
        Object hello = request.getAttribute("hello");
        Object world = request.getAttribute("world");
        Object message = request.getAttribute("message");

        map.put("reqMethod_msg",msg1);
        map.put("annotation",msg);
        map.put("hello",hello);
        map.put("world",world);
        map.put("message",message);
        return map;
    }

}
